﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASPNETPerformance.App_BusinessLogic.BasePages;
using ASPNETPerformance.App_BusinessLogic.StaticPage;

namespace ASPNETPerformance._09_Test {
    public partial class Stopwatch : BasePage {
        const int Iterations = 50000;
        string htmlWrite = string.Empty;

        protected void Page_Load( object sender, EventArgs e ) {
            if ( !this.Page.IsPostBack ) {

                string format = "{foo} is a {bar} is a {baz} is a {qux:#.#} is a really big {fizzle}";
                var o = new {
                    foo = 123,
                    bar = true,
                    baz = "this is a test",
                    qux = 123.45,
                    fizzle = DateTime.Now
                };

                MeasureFormatTime( "Hanselformat", () => o.HanselFormat( format ) );
                MeasureFormatTime( "OskarFormat", () => format.OskarFormat( o ) );
                MeasureFormatTime( "JamesFormat", () => format.JamesFormat( o ) );
                MeasureFormatTime( "HenriFormat", () => format.HenriFormat( o ) );
                MeasureFormatTime( "HaackFormat", () => format.HaackFormat( o ) );

                this.lResult.Text = htmlWrite;
            }
        }

        private void MeasureFormatTime( string name, Action formatAction ) {
            var stopwatch = new System.Diagnostics.Stopwatch();
            stopwatch.Start();
            for ( int i = 0; i < Iterations; i++ ) {
                formatAction();
            }
            stopwatch.Stop();

            htmlWrite += name + " took " + ( ( double )stopwatch.ElapsedMilliseconds / ( double )Iterations ) + " ms" + "<br />";
        }
    }
}